package com.turman.coroutionsample.view

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.turman.coroutionsample.download.DownloadManager
import com.turman.coroutionsample.download.DownloadStatus
import com.turman.coroutionsample.ui.theme.CoroutionSampleTheme
import kotlinx.coroutines.launch
import java.io.File

@Preview
@Composable
fun DownloadFlowPage() {
    var progress by remember {
        mutableStateOf(0f)
    }

    val context = LocalContext.current
    val scope = rememberCoroutineScope()

    CoroutionSampleTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {

                Text(text = "Android Studio下载")
                Spacer(modifier = Modifier.height(50.dp))
                Row(verticalAlignment = Alignment.CenterVertically) {
                    LinearProgressIndicator(
                        progress = progress,
                        color = MaterialTheme.colors.primary
                    )
                    Spacer(modifier = Modifier.width(5.dp))
                    Text(text = "${String.format("%.2f", progress * 100)}%", color = MaterialTheme.colors.primary)
                }

                Spacer(modifier = Modifier.height(20.dp))

                Button(onClick = {
                    scope.launch {
                        val file =
                            File(context.getExternalFilesDir(null)?.path, "android_studio.exe")
                        if (file.exists()) {
                            file.delete()
                        }
                        DownloadManager.download(
                            "https://r3---sn-ni57rn7d.gvt1.com/edgedl/android/studio/install/2022.1.1.20/android-studio-2022.1.1.20-windows.exe?cms_redirect=yes&mh=_v&mip=221.227.201.139&mm=28&mn=sn-ni57rn7d&ms=nvh&mt=1675906596&mv=m&mvi=3&pl=17&rmhost=r1---sn-ni57rn7d.gvt1.com&shardbypass=sd&smhost=r2---sn-ni57rn7k.gvt1.com",
                            file
                        ).collect {
                            when (it) {
                                is DownloadStatus.Progress -> {
                                    progress = it.value
                                    Log.i("buobao", "progress:$progress")
                                }
                                is DownloadStatus.Done -> {
                                    progress = 1.0f
                                    Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show()
                                }
                                is DownloadStatus.Error -> {
                                    Toast.makeText(
                                        context,
                                        it.throwable.message,
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                                else -> {
                                    Toast.makeText(context, "未知错误", Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }) {
                    Text(text = "DOWNLOAD FILE")
                }
                Spacer(modifier = Modifier.height(10.dp))
                Button(onClick = {
                    val file = File(context.getExternalFilesDir(null)?.path, "android_studio.exe")
                    if (file.exists()) {
                        file.delete()
                    }
                    Toast.makeText(context, "文件已删除", Toast.LENGTH_SHORT).show()
                }) {
                    Text(text = "DELETE FILE")
                }
            }
        }
    }
}